package mobi.yuanshen.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import mobi.yuanshen.mapper.EmpMapper;
import mobi.yuanshen.pojo.Emp;
import mobi.yuanshen.pojo.PageBean;
import mobi.yuanshen.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    EmpMapper empMapper;

    public PageBean queryEmps (Integer page,
                               Integer pageSize,
                               String name,
                               Short gender,
                               LocalDate begin,
                               LocalDate end) {
        PageHelper.startPage(page,pageSize); // 开启分页插件
        List<Emp> emps = empMapper.queryEmps(name, gender, begin, end);
        Page p = (Page) emps; // 将List集合强转为Page类型即可
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public Integer delEmp(Integer[] ids) {
        Integer count =  empMapper.delEmp (ids);
        return count;
    }

    @Override
    public void insertEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insertEmp(emp);
    }

    /**
     * 根据员工id查询信息
     * @param id 员工id
     * @return 查询到的信息
     */
    @Override
    public Emp queryEmpById(Integer id) {
        log.info("根据id:{}查询员工信息",id);
        Emp emp = empMapper.queryEmpById (id);
        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp (emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUsernameAndPassword(emp);
    }


}
